package com.xyazm.product.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.common.utils.ParamCheckUtil;
import com.xyazm.product.domain.dto.ProductSkuDTO;
import com.xyazm.product.domain.param.QueryProductSkuListParam;
import com.xyazm.product.domain.param.QueryProductSkuParam;
import com.xyazm.product.exception.ProductBizException;
import com.xyazm.product.service.IProductSkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 商品服务对外暴露的接口实现类
 * @author xuyou
 * 2022/5/15 0015 21:24
*/
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IProductSkuApi.class)
public class ProductSkuApi implements IProductSkuApi {

    @Autowired
    private IProductSkuService productSkuService;

    @Override
    public JsonResult<ProductSkuDTO> getProductSku(QueryProductSkuParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            String skuCode = param.getSkuCode();
            ProductSkuDTO productSkuDTO = productSkuService.getProductSkuBySkuCode(skuCode);
            return JsonResult.buildSuccess(productSkuDTO);
        } catch (ProductBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<ProductSkuDTO>> listProductSkus(QueryProductSkuListParam param) {
        try {
            ParamCheckUtil.checkObjectNonNull(param);
            List<String> skuCodeList = param.getSkuCodeList();
            List<ProductSkuDTO> productSkuDTOList = productSkuService.listProductSkusBySkuCode(skuCodeList);
            return JsonResult.buildSuccess(productSkuDTOList);
        } catch (ProductBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
